package cn.iocoder.yudao.module.design.service.userflowrel;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.design.controller.admin.userflowrel.vo.*;
import cn.iocoder.yudao.module.design.convert.userflowrel.UserFlowRelConvert;
import cn.iocoder.yudao.module.design.dal.dataobject.userflowrel.UserFlowRelDO;
import cn.iocoder.yudao.module.design.dal.mysql.userflowrel.UserFlowRelMapper;
import cn.iocoder.yudao.module.design.enums.design.UserFlowRelStatusCdEnum;
import cn.iocoder.yudao.module.design.enums.user.DesignUserRoleEnum;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.design.enums.ErrorCodeConstants.USER_FLOW_REL_NOT_EXISTS;

/**
 * 用户和流程角色关联 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class UserFlowRelServiceImpl implements UserFlowRelService {

    @Resource
    private UserFlowRelMapper userFlowRelMapper;

    @Override
    public Long createUserFlowRel(UserFlowRelCreateReqVO createReqVO) {
        // 插入
        UserFlowRelDO userFlowRel = UserFlowRelConvert.INSTANCE.convert(createReqVO);
        userFlowRelMapper.insert(userFlowRel);
        // 返回
        return userFlowRel.getId();
    }

    @Override
    public void updateUserFlowRel(UserFlowRelUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateUserFlowRelExists(updateReqVO.getId());
        // 更新
        UserFlowRelDO updateObj = UserFlowRelConvert.INSTANCE.convert(updateReqVO);
        userFlowRelMapper.updateById(updateObj);
    }

    @Override
    public void deleteUserFlowRel(Long id) {
        // 校验存在
        this.validateUserFlowRelExists(id);
        // 删除
        userFlowRelMapper.deleteById(id);
    }

    private void validateUserFlowRelExists(Long id) {
        if (userFlowRelMapper.selectById(id) == null) {
            throw exception(USER_FLOW_REL_NOT_EXISTS);
        }
    }

    @Override
    public UserFlowRelDO getUserFlowRel(Long id) {
        return userFlowRelMapper.selectById(id);
    }

    @Override
    public List<UserFlowRelDO> getUserFlowRelList(Collection<Long> ids) {
        return userFlowRelMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<UserFlowRelDO> getUserFlowRelPage(UserFlowRelPageReqVO pageReqVO) {
        return userFlowRelMapper.selectPage(pageReqVO);
    }

    @Override
    public List<UserFlowRelDO> getUserFlowRelList(UserFlowRelExportReqVO exportReqVO) {
        return userFlowRelMapper.selectList(exportReqVO);
    }

    /**
     * @param vo
     * @return
     */
    @Override
    public List<UserFlowRelDO> getUserFlowRelList(UserFlowRelBaseVO vo) {
        return userFlowRelMapper.selectList(vo);
    }

    @Override
    public List<UserFlowRelDO> getUserFlowRelByProjectManager(Long houseId,Long flowId) {

        QueryWrapper queryUserFlowRel = new QueryWrapper<>();
        queryUserFlowRel.eq("house_id",houseId);
        queryUserFlowRel.eq("flow_id",flowId);
        queryUserFlowRel.eq("role_id", DesignUserRoleEnum.PROJECT_MANAGER.getRoleId());
        queryUserFlowRel.in("status", UserFlowRelStatusCdEnum.USER_REL_STATUS_CD_0.getCode().toString(),
                UserFlowRelStatusCdEnum.USER_REL_STATUS_CD_1.getCode().toString());
        return userFlowRelMapper.selectList(queryUserFlowRel);
    }

}
